/**
 * 
 */
package com.java.test.messagecenter.service.impl;

import com.java.test.audit.service.ExamineManageService;
import com.java.test.mapper.AreaMapper;
import com.java.test.messagecenter.dto.*;
import com.java.test.messagecenter.mapper.*;
import com.java.test.messagecenter.messagepush.dto.SendMailDTO;
import com.java.test.messagecenter.service.MessageCenterService;
import com.java.test.system.registrationtemplate.entity.AreaEntity;
import com.java.test.timer.MessagePushTimer;
import com.java.test.timer.MessagePushTimer;
import com.java.test.user.mapper.PlayAccountMapper;
import com.java.test.util.DateUtil;
import com.java.test.util.JsonUtil;
import com.java.test.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 消息中心
 *
 */
@Service
public class MessageCenterServiceImpl implements MessageCenterService {

	@Autowired
	private NoticeMapper noticeMapper;

	@Autowired
	private PushMessageMapper pushMessageMapper;

	@Autowired
	private AppUpdateMapper appUpdateMapper;

	@Autowired
	private TerminalInfoMapper terminalInfoMapper;

	@Autowired
	AreaMapper areaMapper;

	@Autowired
	private HorseNoticeMapper horseNoticeMapper;

	@Autowired
	PlayAccountMapper playAccountMapper;

	@Autowired
	ExamineManageService examineManageService;

	@Override
	public PageInfo<NoticeInfo> selectpushnoticeList(PageInfo<NoticeInfo> page) {
		NoticeInfo info = page.getInfo();

		int total = noticeMapper.selectpushnoticeCount(info);

		List<NoticeInfo> list = noticeMapper.selectpushnoticeList(info, page);

		page.setTotalCount(total);

		page.setList(list);

		return page;
	}

	@Override
	public int updatepushnoticeInfo(NoticeInfo info) {
		return noticeMapper.updatepushnoticeInfo(info);
	}

	@Override
	public int deletepushnoticeInfo(int[] ids) {
		int i = noticeMapper.deletepushnoticeInfo(ids);
		if (i > 0) {
			for (int a = 0; a < ids.length; a++) {
				MessagePushTimer.idListStop.add(ids[a]);
				MessagePushTimer.pageAnnouncementListStop.add(ids[a]);
			}
		}
		return i;
	}

	@Override
	public PageInfo<PushMessageInfo> selectpushmessageList(PageInfo<PushMessageInfo> page) {
		PushMessageInfo info = page.getInfo();

		int total = pushMessageMapper.selectpushmessageCount(info);

		List<PushMessageInfo> list = pushMessageMapper.selectpushmessageList(info, page);

		list.forEach(b -> {
			if (b.getPushItemAccount() == null || "".equals(b.getPushItemAccount())) {
				b.setPushItemAccount(String.valueOf(0));
				return;
			}
			String str[] = b.getPushItemAccount().split(",");
			b.setPushItemAccount(String.valueOf(str.length));

		});

		page.setTotalCount(total);

		page.setList(list);

		return page;
	}

	@Override
	public PushMessageInfo selectpushmessagebyId(PushMessageInfo info) {
		return pushMessageMapper.selectpushmessagebyId(info);
	}

	@Override
	public boolean handlepushMessage() {

		LocalDateTime nowTime = LocalDateTime.now();

		List<PushMessageInfo> list = pushMessageMapper.selectpushmessageTimer(nowTime);

		if (list == null || list.size() <= 0) {
			return false;
		}

		for (PushMessageInfo b : list) {
			if (b.getPushTime() == null)
				continue;
			boolean isSame = DateUtil.dateAmorethandateB(nowTime, b.getPushTime());

			if (isSame) {
				// 新增推送消息逻辑 ---- 调推送接口

			}

		}

		return true;
	}

	@Override
	public int addpushnoticeInfo(NoticeInfo info) {

		int i = noticeMapper.addpushnoticeInfo(info);

		// 及时推送公告
		if (i > 0) {
			// 调用 -- 推送接口

		}
		return i;
	}

	@Override
	public List<TerminalInfo> selectallchannelByterminalnName(String terminalName) {
		return terminalInfoMapper.selectallchannelByterminalnName(terminalName);
	}

	@Override
	public PageInfo<TerminalInfo> selectterminalPage(PageInfo<TerminalInfo> page) {
		TerminalInfo info = page.getInfo();

		int total = terminalInfoMapper.selectterminalCount(info);

		List<TerminalInfo> list = terminalInfoMapper.selectterminalList(info, page);

		list.forEach(b -> {

			String areaids = b.getAreaId();

			if (areaids == null || "".equals(areaids))
				return;

			List<AreaEntity> arealist = areaMapper.selectAreaNamebyId(areaids);

			if (arealist == null || arealist.size() <= 0)
				return;

			String areaName = "";

			for (AreaEntity c : arealist) {
				areaName += c.getName() + ",";
			}
			areaName = areaName.substring(0, areaName.length() - 1);

			b.setAreaName(areaName);

		});

		page.setTotalCount(total);

		page.setList(list);

		return page;
	}

	@Override
	public TerminalInfo selectterminalbyId(TerminalInfo info) {
		return terminalInfoMapper.selectterminalbyId(info);
	}

	@Override
	public TerminalInfo selectterminalbychannelId(String channelId) {
		return terminalInfoMapper.selectterminalbychannelId(channelId);
	}

	@Override
	public int insertterminalInfo(TerminalInfo info) {
		return terminalInfoMapper.insertterminalInfo(info);
	}

	@Override
	public int updateterminalInfo(TerminalInfo info) {

		return terminalInfoMapper.updateterminalInfo(info);
	}

	@Override
	public int deleteminalInfo(TerminalInfo info) {
		return terminalInfoMapper.deleteminalInfo(info);
	}

	@Override
	public PageInfo<AppUpdateInfo> selectappupdatePage(PageInfo<AppUpdateInfo> page) {

		AppUpdateInfo info = page.getInfo();

		int total = appUpdateMapper.selectappupdateCount(info);

		List<AppUpdateInfo> list = appUpdateMapper.selectappupdateList(info, page);

		if (list != null && list.size() > 0) {

			list.forEach(b -> {
				String channelid = b.getChannelId();
				List<TerminalInfo> chanList = areaMapper.selectterminalbyIdInfo(channelid);

				if (chanList == null || chanList.size() <= 0)
					return;

				String chanName = "";
				for (TerminalInfo cinfo : chanList) {
					chanName += cinfo.getTchannelId() + ",";
				}
				chanName = chanName.substring(0, chanName.length() - 1);
				b.setChannelidStr(chanName);
			});
		}

		page.setTotalCount(total);

		page.setList(list);

		return page;
	}

	@Override
	public int updateappupdateInfo(AppUpdateInfo info) {
		return appUpdateMapper.updateappupdateInfo(info);
	}

	@Override
	public int insertappupdateInfo(AppUpdateInfo info) {
		return appUpdateMapper.insertappupdateInfo(info);
	}

	@Override
	public int insertpushmessageInfo(PushMessageInfo info) {

		// 添加推送的时候需要拿到所有用户列表方便推送
		int messageType = info.getPushitemType();

		List<String> pushUser = new ArrayList<String>();

		String pushitem = info.getPushitemContent();

		String itemArray[] = pushitem.split(",");

		// 条件推送
		if (messageType == 2) {
			for (int i = 0; i < itemArray.length; i++) {
				String account = examineManageService.getGameUniqueNumber(itemArray[i]);
				if (account != null && !"".equals(account)) {
					pushUser.add(account);
				}
			}
			// 画像推送
		} else if (messageType == 1) {

			int areaid = Integer.valueOf(itemArray[0]);

			// 分层
			int sliceid = Integer.valueOf(itemArray[1]);

			// 等级
			int gradeid = Integer.valueOf(itemArray[2]);

			String channelid = itemArray[3];

			int cunkuan = Integer.valueOf(itemArray[4]);

			int tikuan = Integer.valueOf(itemArray[5]);

			// 根据条件推送需要发送的用户

			String sql = "";

			if (tikuan == 0 && cunkuan == 0) {
				sql = "select accountName from member_play_account a where 1=1";
			} else if (cunkuan != 0 && tikuan != 0) {
				sql = "select accountName from member_play_account a,"
						+ "member_deposit b,member_reflect c where  a.account=b.game_account and a.account=c.game_account";
			} else if (cunkuan != 0) {
				sql = "select accountName from member_play_account a,member_deposit b where a.account=b.game_account";
			} else if (tikuan != 0) {
				sql = "select accountName from member_play_account a,member_reflect c where a.account=c.game_account";
			}

			if (areaid != 0) {
				sql += " and a.area_id='" + areaid + "'";
			}

			if (sliceid != 0) {
				sql += " and a.slice_value='" + sliceid + "'";
			}

			if (gradeid != 0) {
				sql += " and a.grade_value='" + gradeid + "'";
			}

			if (!"0".equals(channelid)) {
				sql += " and a.register_channel='" + channelid + "'";
			}

			pushUser = playAccountMapper.selectpushUserList(sql);
		}
		if (pushUser.size() > 0) {
			try {
				info.setPushItemAccount(JsonUtil.obj2json(pushUser));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return pushMessageMapper.insertpushmessageInfo(info);
	}

	@Override
	public NoticeInfo selectnoticeInfo(NoticeInfo info) {
		return noticeMapper.selectnoticeInfo(info);
	}

	@Override
	public AppUpdateInfo selectappupdateInfo(AppUpdateInfo info) {
		return appUpdateMapper.selectappupdateInfo(info);
	}

	@Override
	public int deleteappupdateInfo(AppUpdateInfo info) {
		return appUpdateMapper.deleteappupdateInfo(info);
	}

	@Override
	public PageInfo<HorseNoticeInfo> selecthorsenoticePage(PageInfo<HorseNoticeInfo> page) {
		HorseNoticeInfo info = page.getInfo();

		int total = horseNoticeMapper.selecthorsenoticeCount(info);

		List<HorseNoticeInfo> list = horseNoticeMapper.selecthorsenoticeList(info, page);

		// 更新跑马灯状态
		if (list != null && list.size() > 0) {
			list.forEach(b -> {

				LocalDateTime startTime = b.getStartTime();
				LocalDateTime endTime = b.getEndTime();
				LocalDateTime nowTime = LocalDateTime.now();

				int cura = nowTime.compareTo(startTime);
				int curb = nowTime.compareTo(endTime);

				if (cura < 0) {
					b.setStatus(1);
				} else {
					if (curb < 0) {
						b.setStatus(2);
					} else {
						b.setStatus(3);
					}
				}
			});
		}

		page.setTotalCount(total);

		page.setList(list);

		return page;
	}

	@Override
	public HorseNoticeInfo selecthorsenoticeInfo(HorseNoticeInfo info) {
		return horseNoticeMapper.selecthorsenoticeInfo(info);
	}

	@Override
	public int deletehorsenoticeInfo(long[] ids) {
		return horseNoticeMapper.deletehorsenoticeInfo(ids);
	}

	@Override
	public int inserthorsenoticeInfo(HorseNoticeInfo info) {
		return horseNoticeMapper.inserthorsenoticeInfo(info);
	}

	@Override
	public int updatehorsenoticeInfo(HorseNoticeInfo info) {
		return horseNoticeMapper.updatehorsenoticeInfo(info);
	}

}
